$(function () {
  
  // Get the form.
  var form = $('#contact-form');
  console.log(form)
  // Get the messages div.
  var formMessages = $('.form-message');
  // Set up an event listener for the contact form.
  $(form).submit(function (e) {
    // Stop the browser from submitting the form.
    e.preventDefault();
    // Serialize the form data.
    var formData = $(form).serialize();
    console.log(formData)
    let formSplit = formData.split("&");
    let data = {};
    formSplit.map(item => {
      let itemS = item.split('=');
      data[itemS[0]] = decodeURIComponent(itemS[1]);
    })
    console.log(data)
    $(formMessages).text("错误信息");
    $.ajax({
      type: 'POST',
      headers: {
        "Content-Type": "application/json;charset=UTF-8"
      },
      url: 'https://contact.wangjintech.cn/admin-api/system/contact-us/create',
      dataType: 'json',
      contentType: 'application/json;charset=UTF-8',
      data: JSON.stringify(data),
      success: function (data) {
        if (data.code === 0) {
          alert("发送成功")
        }
      }
    }).done(function (response) {
      // Make sure that the formMessages div has the 'success' class.
      $(formMessages).removeClass('error');
      $(formMessages).addClass('success');
      
      // Set the message text.
      $(formMessages).text(response);
      
      // Clear the form.
      $('#contact-form input, #contact-form textarea').val('');
    }).fail(function (data) {
      // Make sure that the formMessages div has the 'error' class.
      $(formMessages).removeClass('success');
      $(formMessages).addClass('error');
      
      // Set the message text.
      if (data.responseText !== '') {
        $(formMessages).text(data.responseText);
      } else {
        $(formMessages).text('Oops! An error occurred and your message could not be sent.');
      }
    });
  });
  
});